import java.util.Scanner;

/**
 * @author gao
 * @date 2025/7/6 22:33
 */

class ListNode {
    int val;
    ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        // 计算链表长度
        int n = 0;
        ListNode cur = head;
        while (cur != null) {
            n++;
            cur = cur.next;
        }
        // 计算需要反转的组数
        n /= k;

        ListNode newHead = new ListNode(0);
        ListNode prev = newHead;
        cur = head;

        // 遍历链表，反转每组k个节点
        for (int i = 0; i < n; i++) {
            // tmp指向当前组的第一个节点
            ListNode tmp = cur;
            // 反转当前组的k个节点
            for (int j = 0; j < k; j++) {
                // next指向当前节点的下一个节点
                ListNode next = cur.next;
                // 将当前节点的next指向prev的next
                cur.next = prev.next;
                // 将prev的next指向当前节点
                prev.next = cur;
                // 将cur指向next
                cur = next;
            }
            // prev指向当前组的第一个节点
            prev = tmp;
        }
        // 将prev的next指向剩余的节点
        prev.next = cur;
        return newHead.next;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            ListNode head = null;
            ListNode tail = null;
            for (int i = 0; i < n; i++) {
                int val = in.nextInt();
                ListNode newNode = new ListNode(val);
                if (head == null) {
                    head = newNode;
                    tail = newNode;
                } else {
                    tail.next = newNode;
                    tail = newNode;
                }
            }
            int k = in.nextInt();
            Solution solution = new Solution();
            Print(solution.reverseKGroup(head, k));
        }
    }

    private static void Print(ListNode head) {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
}